package com.pan.controller.Operations;

import com.pan.aop.BusinessType;
import com.pan.aop.MyLog;
import com.pan.controller.tools.Methods;
import com.pan.controller.tools.R1;
import com.pan.domain.Apply;
import com.pan.domain.User;
import com.pan.service.IManagerService;
import com.pan.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/applychange")
@Tag(name = "ApplyController",description = "审核申请")
public class ApplyController {
    @Autowired
    private IManagerService managerService;
    @Autowired
    private IUserService userService;

    Methods methods = new Methods();

    //审核申请
    @Operation(summary = "操作人员或管理员审批申请")
    @PutMapping
    @MyLog(title = "操作人员或管理员审批申请",optParam = "#{apply}",businessType = BusinessType.UPDATE)
    public R1 ApplyManager(@Parameter(description = "申请信息,为一个Json字符串",required = true) @RequestBody Apply apply,
                           HttpServletRequest request){
        //检查用户权限
        if (methods.isRenShiOrManager(request,userService)){
            //查询审批状态,apply1为实际的申请信息
            User user = userService.getByName(apply.getName());
            Apply apply1 = userService.getOwn(user);
            if(apply1.getDelete()==0){
                return new R1(false,"该申请已审批！");
            }else {
                //将审批状态码由1改为0
                managerService.changeApplyCondition(apply);
                return new R1(true,managerService.applyManager(apply1));
            }
        }
        return new R1(false,"权限不足");
    }
}
